package com.duanjw.java.valatile;

import java.util.concurrent.TimeUnit;

public class ReaderAndUpdater {
    final static int MAX = 5;
    volatile static  int initValue  = 0 ;
    public static void main(String[] args) {
        new Thread(()->{
            int localValue = initValue;
            while (localValue < MAX) {
                if(localValue < MAX) {
                    System.out.println("initValue reader:" + initValue);
                    localValue = initValue;
                }
            }
        }, "reader").start();

        new Thread(()->{
            int localValue = initValue;
            while (localValue < MAX) {
                System.out.println("initValue updater:" + (++localValue));
                initValue = localValue;
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "updater").start();
    }
}
